<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card class="tree-card">
        <!-- 头部布局 -->
        <TreeTools :tree-data="compoany" @addDept="addDept" />
        <el-tree :data="departs" :props="defaultProps">
          <TreeTools slot-scope="{data}" :tree-data="data" :is-root="false" @addDept="addDept" @update="initData" @editDept="editDept" />
        </el-tree>
      </el-card>
    </div>
    <AddDept ref="addDept" :node-data="nodeData" @update="initData" />
  </div>
</template>

<script>
import { tranListToTreeData } from '@/utils'
import { getDepartmentListData } from '@/api/department'
import TreeTools from './components/tree-tools.vue'
import AddDept from './components/add-dept.vue'

export default {
  components: {
    TreeTools,
    AddDept
  },
  data() {
    return {
      nodeData: null,
      defaultProps: {
        label: 'name'
      },
      departs: [],
      compoany: {
        name: '',
        manager: '负责人',
        id: ''
      }
    }
  },
  created() {
    this.initData()
  },
  methods: {
    async initData() {
      const { depts, companyName } = await getDepartmentListData()
      console.log(depts)
      this.departs = tranListToTreeData(depts, '')
      this.compoany.name = companyName
    },
    addDept(value) {
      console.log(this.$refs.addDept)
      this.$refs.addDept.isShow = true
      this.nodeData = value
    },
    async editDept(val) {
      // 获取该行的数据
      await this.$refs.addDept.getgetDepartmentInfo(val.id)
      this.$refs.addDept.isShow = true
      this.nodeData = val
    }
  }
}
</script>

<style lang="scss" scoped>
.tree-card{
  padding: 30px 140px;
  font-size: 14px;
}
</style>
